package com.xie.controller;

import com.xie.pojo.Sclass;
import com.xie.service.SclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sclass")
public class SclassController {

    @Autowired
    @Qualifier("SclassServiceImpl")
    private SclassService sclassService;

    @RequestMapping("/allSclass")
    public String allSclass(HttpSession session){
        List<Sclass> sclasses = sclassService.queryAllSclass();
        session.setAttribute("sclassList",sclasses);
        return "sclass";
    }

    @RequestMapping("/deleteSclass")
    public String deleteSclass(int classid, Model model){
        int i = sclassService.deleteSclass(classid);
        if(i > 0){
            return "redirect:/sclass/allSclass";
        }else{
            model.addAttribute("msg","删除失败");
            return "redirect:/sclass/allSclass";
        }
    }

    @RequestMapping("/toAddSclass")
    public String toAddSclass(){
        return "addSclass";
    }
    @RequestMapping("addSclass")
    public String addSclass(Sclass sclass,Model model){
        int i = sclassService.addSclass(sclass);
        if (i > 0){
            return "redirect:/sclass/allSclass";
        }else{
            model.addAttribute("msg","添加失败");
            return "redirect:/sclass/toAddSclass";
        }
    }

    @RequestMapping("/toUpdateSclass")
    public String toUpdateSclass(int classid,Model model){
        Sclass sclass = sclassService.querySclass(classid);
        model.addAttribute("QSclass",sclass);
        return "updateSclass";
    }
    @RequestMapping("/updateSclass")
    public String updateSclass(Sclass sclass,Model model){
        int i = sclassService.updateSclass(sclass);
        if (i > 0){
            return "redirect:/sclass/allSclass";
        }else{
            model.addAttribute("msg","修改失败");
            return "redirect:/sclass/allSclass";
        }
    }











}
